
	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Create all relevant variables

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// load data

	use "$Endline_dtDeID/data_deidentified.dta", clear

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Keep relevant data in terms of teachers.

	drop if exclude_nonteachers==1

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Some variables will be inverted. To avoid confusion take out all value labels

	label drop _all

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Tests

	forval x=3/5{
		gen test_pt`x' = test_score if test_grade==`x' & test_type==1
		gen test_mat`x' = test_score if test_grade==`x' & test_type==2
		gen test_`x' = test_score if test_grade==`x'
	}

	gen test_34 = test_score if inlist(test_grade,3,4)

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Parents

	gen P_interview = 1 if P_ss_key!=""
	gen P_game = 1 if Gp_orange_id!=.

	gen P_sample = 1 if P_interview==1 | P_game==1

	rename P01_01  P_age
	rename P01_02  P_female
	rename P01_13  P_children

	rename P02_01  P_student_age
	rename P02_02  P_student_female

	gen P_primedu = 1 if P01_06>2 & P01_06!=.
	replace P_primedu = 0 if P_primedu==. & P01_06!=.

	gen P_partner = P01_08
	recode P_partner (2=1) (3=0) (4=0) (5=0)

	gen P_parent = 0 if P_resp_relation!=.
	replace P_parent = 1 if P_resp_relation==1

	forval x=3/5{
	gen P_student_grade_`x' = 0 if P_student_grade!=.
	replace P_student_grade_`x' = 1 if P_student_grade==`x'
	}

	gen P_pt = 0 if P01_18!=.
	replace P_pt = 1 if P01_18==1

	gen P_rules = P03_02
	gen P_sleep = P03_03

	foreach var in P_rules P_sleep{
	recode `var' (1=5) (2=4) (4=2) (5=1)
	}

	rename P03_05 P_homework
	rename P03_07 P_dinner

	gen P_talk_teacher 	= P03_22
	gen P_info 			= P03_21
	gen P_parti 		= P03_24

	foreach var in P_info P_talk_teacher P_parti{
	recode `var' (1=7) (2=6) (3=5) (5=3) (6=2) (7=1)
	}

	rename P04_(##) P_problem(##)
	rename P_problem0* P_problem*

	foreach var in P_problem4 P_problem6 P_problem7 P_problem8 ///
	P_problem17 ///
	P_problem19 P_problem21 P_problem22 ///
	P_problem28 P_problem29 P_problem30 P_problem31 P_problem32 {
	recode `var' (0=3) (1=2) (2=1) (3=0)
	}

	rename P06_03 P_director_respons
	rename P06_06 P_school_good
	rename P06_07 P_teacher_good
	rename P06_10 P_perf_teachers
	rename P06_14 P_perf_student

	foreach var in  P_teacher_good P_perf_teachers P_director_respons P_school_good P_perf_student{
	recode `var' (1=5) (2=4) (4=2) (5=1)
	}

	rename P09_01 P_social

	rename P01_20 P_timetoschool

	egen P_assets = rowtotal(P11_03_01 P11_03_02 P11_03_03 P11_03_04 P11_03_05 P11_03_06 P11_03_07 P11_03_08 P11_03_09 P11_03_10 P11_03_11 P11_03_12 P11_03_13 P11_03_14 P11_03_15 P11_03_16 P11_03_17 P11_03_18 P11_03_19 P11_03_20), missing

	rename Gp_version Gp_treatment

	rename Np* Gp*

	rename Gp01 Gp_knows_parents_d
	lab var Gp_knows_parents_d "Participant knows at least one other parent"

	rename Gp02 Gp_knows_parents
	lab var Gp_knows_parents "Number of other parents participant knows"
	replace Gp_knows_parents = 0 if Gp_knows_parents_d==0

	rename Gp05 Gp_knows_teachers_d
	lab var Gp_knows_teachers_d "Participant knows at least one teacher"

	rename Gp06 Gp_knows_teachers
	lab var Gp_knows_teachers "Number of teachers participant knows"
	replace Gp_knows_teachers = 0 if Gp_knows_teachers_d==0

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Teachers

	gen T_interview = 1 if TD_ss_key!=""
	gen T_demo = 1 if TDs_edu!=.

	gen T_sample = 1 if T_interview==1 | T_demo==1

	replace TD_female = 1 if teacher_id2==977
	replace TD_female = 1 if teacher_id2==1409
	replace TD_female = 1 if teacher_id2==2977
	replace TD_female = 0 if teacher_id2==3208
	replace TD_female = 0 if teacher_id2==4911
	replace TD_female = 0 if teacher_id2==10606
	replace TD_female = 0 if teacher_id2==10601
	replace TD_female = 0 if teacher_id2==12477

	rename TD_* T_*

	rename T_resp_g* T_teacher_g*
	rename *grade__* *grade_*
	rename T_teacher_grade_0 T_teacher_preschool
	rename T_teacher_grade_11 T_teacher_module1
	rename T_teacher_grade_22 T_teacher_module2
	rename T_teacher_grade_33 T_teacher_module3

	rename TDs_edu T_edu_school
	recode T_edu_school (8=7) (9=8) (10=8) (11=8) (12=8)
	gen T_edu_comb = T_edu
	recode T_edu_comb (9=8) (10=8) (11=8)
	replace T_edu_comb = T_edu_school if T_edu_comb==.

	gen T_highedu = 0 if T_edu_comb!=.
	replace T_highedu = 1 if inlist(T_edu_comb,6,7,8)

	gen T_pt = 0 if T_language!=.
	replace T_pt = 1 if T_language==1

	replace T_children = 2 if T_children==42
	replace T_children = 7 if T_children==477

	gen T_partner = T_marital
	recode T_partner (2=1) (3=0) (4=0) (5=0)

	egen T_exp_teach = rowtotal(T_teach_this_school_years T_teach_other_school_years D_taught_this_school_years) if T_interview==1

	forval i=1/3{
		gen  p_inmeeting_`i'		= TD03_08_0`i' if inlist(TD03_01_01_0`i',1,2,3,4,5,6)
	}
		egen p_inmeeting = rowmean(p_inmeeting_1 p_inmeeting_2 p_inmeeting_3)

	rename TD07_(##) T_problem(##)
	rename T_problem0* T_problem*

	foreach var in  T_problem4 T_problem6 T_problem7 T_problem8 ///
									T_problem16 ///
									T_problem28 T_problem29 T_problem30 T_problem31 T_problem32{
				recode `var' (0=3) (1=2) (2=1) (3=0)
	}

	foreach var in T_parents_involvement T_parents_availablity T_perf_comm {
	recode `var' (1=5) (2=4) (4=2) (5=1)
	}

	rename TD12_01 T_social

	egen T_assets = rowtotal(TD14_03_01 TD14_03_02 TD14_03_03 TD14_03_04 TD14_03_05 TD14_03_06 TD14_03_07 TD14_03_08 TD14_03_09 TD14_03_10 TD14_03_11 TD14_03_12 TD14_03_13 TD14_03_14 TD14_03_15 TD14_03_16 TD14_03_17 TD14_03_18 TD14_03_19 TD14_03_20), missing

	rename Gtd* Gt*
	rename Ntd* Gt*

	rename Gt_tg_return200 Gt_tg200
	rename Gt_tg_return400 Gt_tg400
	rename Gt_tg_return600 Gt_tg600
	rename Gt_tg_return800 Gt_tg800

	foreach var in Gt_tg200 Gt_tg400 Gt_tg600 Gt_tg800{
		gen `var'r = Gt_tg200
	}

	replace Gt_tg200r = Gt_tg200r/600
	replace Gt_tg400r = Gt_tg400r/1200
	replace Gt_tg600r = Gt_tg600r/1800
	replace Gt_tg800r = Gt_tg800r/2400

	egen Gt_tg = rowmean(Gt_tg200r Gt_tg400r Gt_tg600r Gt_tg800r)

	rename Gt_version Gt_treatment

	rename Gt01 Gt_knows_teachers_d
	lab var Gt_knows_teachers_d "Participant knows at least one other teacher"

	rename Gt02 Gt_knows_teachers
	lab var Gt_knows_teachers "Number of other teachers participant knows"
	replace Gt_knows_teachers = 0 if Gt_knows_teachers_d==0

	rename Gt05 Gt_knows_parents_d
	lab var Gt_knows_parents_d "Participant knows at least one parent"

	rename Gt06 Gt_knows_parents
	lab var Gt_knows_parents "Number of parents participant knows"
	replace Gt_knows_parents = 0 if Gt_knows_parents_d==0

	replace Gt_pgg_belief = Gt_pgg_belief + Gt_pgg if Gt_pgg_belief_old==1

	egen pres_teacher = rowmean(pres_teacher1 pres_teacher2)


	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// School level data

	// Director

	rename D01_01 D_age
	rename D01_02 D_female

	rename D01_12 D_partner
	recode D_partner (2=1) (3=0) (4=0) (5=0)

	rename D01_17 D_children

	rename D07_(##) D_problem(##)
	rename D_problem0* D_problem*

	foreach var in D_problem3 D_problem4 D_problem5 D_problem6 D_problem7 D_problem8 D_problem9 D_problem10 D_problem11 D_problem12 D_problem13 D_problem14 D_problem15 D_problem16 D_problem17 D_problem18 D_problem19 D_problem20 D_problem21 D_problem22 D_problem23 D_problem24 D_problem25 D_problem26 D_problem27 D_problem28 D_problem29 D_problem30 D_problem31 D_problem32{
	recode `var' (0=3) (1=2) (2=1) (3=0)
	}

	rename D09_08 D_perf_teachers
	recode D_perf_teachers (1=5) (2=4) (4=2) (5=1)

	rename D12_01 D_social

	egen D_assets = rowtotal(D14_03_01 D14_03_02 D14_03_03 D14_03_04 D14_03_05 D14_03_06 D14_03_07 D14_03_08 D14_03_09 D14_03_10 D14_03_11 D14_03_12 D14_03_13 D14_03_14 D14_03_15 D14_03_16 D14_03_17 D14_03_18 D14_03_19 D14_03_20), missing

	gen D_pt = 0 if D01_22!=.
	replace D_pt = 1 if D01_22==1

	gen D_highedu = 0 if D01_06!=.
	replace D_highedu = 1 if inlist(D01_06,6,7,8,9,10)

	// School

	rename S06_05_02__1 S_info1
	rename S06_05_02__2 S_info2
	rename S06_05_02__3 S_info3
	rename S06_05_02__4 S_info4
	rename S06_05_02__5 S_info5
	rename S06_05_02__6 S_info6
	rename S06_05_02__7 S_info7
	rename S06_05_02__8 S_info8

	rename S07_01 S_teachers

	rename S03_01__2 S_new_exerbooks
	rename S03_01__7 S_new_chalk

	rename S08_10__2 S_board_parent_rep

	rename S09_02 S_comm
	rename S09_10 S_comm_members
	replace S_comm_members = 0 if S_comm==0

	gen I_chair_desk_broken_sh = I_chair_desk_broken/I_chair_desk
	replace I_chair_desk_broken_sh = 0 if I_chair_desk_broken_sh==.

	gen I_chair_desk_usable_sh = 1 - I_chair_desk_broken_sh

	gen bathroom_students = I_bathrooms_students
	replace bathroom_students = 1 if bathroom_students>1 & bathroom_students!=.

	// Admin data

	rename A17* A*_17

	rename A18_establishment_year A_establishment_year

	rename A18_distance_municipal_center A_distance_municipal_center

	order school_id teacher_id parent_id teacher_id2 parent_id2

	gen classrooms = A_classrooms_17

	rename A18_* A_*

	gen distance_mc = A_distance_municipal_center
	replace distance_mc = S_school_distance if distance_mc==.

	gen schoolage = 2015 - A_establishment_year

	* ******************************************************************************
	* Cleaning based on GPS coordinates
	* ******************************************************************************

	replace distance_mc  = 0 if school_id==87
	replace distance_mc  = 1 if school_id==77
	replace distance_mc  = 2 if school_id==73
	replace distance_mc = 38 if school_id==94
	replace distance_mc = 30 if school_id==109
	replace distance_mc = . if school_id==82
	replace distance_mc = . if school_id==82


	// solve problem with student attendance (most students show up more than once because of the two tests)

	gsort school_id parent_id
	by school_id parent_id: gen n =_n
	replace n = . if n!=1 | parent_id==.

	replace pres_stud1 = pres_stud1*n
	replace pres_stud2 = pres_stud2*n

	drop n



	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Add baseline variables

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	gen t = 1
	order t

	append using "$Baseline_dtFin/data_baseline.dta"

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Rename important regression variables

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	rename cluster block

	rename municipality V

	tab treatment, gen(T)
	rename T1 T0
	rename T2 T1
	rename T3 T2
	rename T4 T3

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Regression indicators

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	sort t school_id teacher_id2

	by t school_id teacher_id2: gen regteacher = _n
	replace regteacher = 0 if regteacher >1
	replace regteacher = 0 if teacher_id==77
	replace regteacher = 0 if teacher_id==.
	replace regteacher = 0 if T_sample!=1
	replace regteacher = 0 if T_age==.

	sort t school_id parent_id2
	by t school_id parent_id2: gen regparent = _n
	replace regparent = 0 if regparent >1
	replace regparent = 0 if parent_id==.
	replace regparent = 0 if P_sample!=1

	sort t school_id
	by t school_id: gen regschool = _n
	replace regschool = 0 if regschool >1

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Standardize test scores

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	foreach var in test_pt3 test_mat3 test_pt4 test_mat4 test_pt5 test_mat5 pres_stud1{
		sum `var'
		gen `var'_z = (`var'-r(mean))/r(sd)
	}

	forval x=3/4{
		gen test_`x'_z = test_mat`x'_z
		replace test_`x'_z = test_pt`x'_z if test_`x'_z==.
	}

	gen test_34_z = test_3_z
	replace test_34_z = test_4_z if test_34_z==.

	foreach var in test_34_z test_3_z test_4_z pres_stud1_z{
		sum `var' if T0==1
		replace `var' = (`var'-r(mean))/r(sd)
	}

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Final school level variable creation

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	bysort school_id: egen studentsbaseline = max(SQ4_students)
	replace A_students_17 = A_students if t==0
	replace studentsbaseline = A_students_17 if studentsbaseline==.

	gen students = A_students
	replace students = A_students_17 if students==.

	replace students = studentsbaseline if t==0

	gen teachers = S_teachers
	replace teachers = A_teachers if t==0

	replace classrooms = A_classrooms if t==0

	gen teachersbaseline = A_teachers_17
	gen classroomsbaseline = A_classrooms_17







	replace A_highest_grade = A_highest_grade_17 if t==1
	gen has79 = 0 if A_highest_grade!=.
	replace has79 = 1 if A_highest_grade>6

	sort school_id
	by school_id: egen distance_mcX = max(distance_mc)
	by school_id: egen urbanX = max(urban)
	by school_id: egen schoolageX = max(schoolage)

	replace distance_mc = distance_mcX
	replace urban = urbanX
	replace schoolage = schoolageX
	replace schoolage = schoolage+3 if t==1

	drop distance_mcX urbanX schoolageX

	forval x=1/6{
		gen A_repeat_share`x' = (A_repeaters_g`x'/A_students_g`x')
	}

	egen A_repeat_share = rowmean(A_repeat_share1 A_repeat_share2 A_repeat_share3 A_repeat_share4 A_repeat_share5 A_repeat_share6)

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Standardize variables so that they can be combined into indices

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Parent level

	foreach var in P_homework P_dinner P_rules P_sleep P_talk_teacher P_info P_problem19 P_problem21 P_problem22 P_teacher_good P_perf_teachers P_problem4 P_problem6 P_problem7 P_problem8 P_problem28 P_problem29 P_problem30 P_problem31 P_problem32 P_problem17 P_director_respons P_school_good {
		sum `var' if regparent==1 & t==1
		gen `var'_z2 = (`var'-r(mean))/r(sd)
	}

	// Teacher level

	foreach var in T_parents_involvement T_problem16 T_parents_availablity p_inmeeting T_perf_comm T_problem4 T_problem6 T_problem7 T_problem8 T_problem28 T_problem29 T_problem30 T_problem31 T_problem32{
		sum `var' if regteacher==1 & t==1
		gen `var'_z2 = (`var'-r(mean))/r(sd)
	}

	// School level

	sort school_id
	by school_id: egen P_assembly = mean(P_parti)

	foreach var in P_assembly S_comm_members S_comm S_board_parent_rep D_problem19 D_problem21 D_problem22 D_perf_teachers S_info1 S_info2 S_info3 S_info4 S_info5 S_info6 S_info7 S_info8 I_classrooms_improvement I_security I_fence I_fence_impr_const I_cleaner bathroom_students A_water I_chair_desk_usable_sh D_problem4 D_problem6 D_problem7 D_problem8 D_problem28 D_problem29 D_problem30 D_problem31 D_problem32 S_new_exerbooks S_new_chalk{
		sum `var' if regschool==1 & t==1
		gen `var'_z2 = (`var'-r(mean))/r(sd)
	}


	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Create and standardize indices - demean using control group

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	// Parent level

	egen parentonT = rowmean(P_problem19_z2 P_problem21_z2 P_problem22_z2 P_teacher_good_z2 P_perf_teachers_z2)
	egen parentmob1 = rowmean(P_homework_z2 P_dinner_z2 P_rules_z2 P_sleep_z2)
	egen parentmob2 = rowmean(P_talk_teacher_z2 P_info_z2)
	egen parentsonF = rowmean(P_problem4_z2 P_problem6_z2 P_problem7_z2 P_problem8_z2 P_problem28_z2 P_problem29_z2 P_problem30_z2 P_problem31_z2 P_problem32_z2)
	egen parentsonM = rowmean(P_problem17_z2 P_director_respons_z2 P_school_good_z2)

	foreach var in parentonT parentmob1 parentmob2 parentsonF parentsonM P_school_good P_perf_student P_homework P_dinner P_rules P_sleep{
	sum `var' if regparent==1 & t==1 & T0==1
	gen `var'_z = (`var'-r(mean))/r(sd)
	}

	// Teacher level

	egen parentmobT = rowmean(T_parents_involvement_z2 T_problem16_z2 T_parents_availablity_z2 p_inmeeting_z2 T_perf_comm_z2)
	egen teacheronF = rowmean(T_problem4_z2 T_problem6_z2 T_problem7_z2 T_problem8_z2 T_problem28_z2 T_problem29_z2 T_problem30_z2 T_problem31_z2 T_problem32_z2)
	gen Gt_dg_r = Gt_dg/2000
		gen Gt_dg_r1 = Gt_dg_r if Gt_treatment==1

	foreach var in parentmobT teacheronF Gt_dg_r1 pres_teacher {
	sum `var' if regteacher==1 & t==1 & T0==1
	gen `var'_z = (`var'-r(mean))/r(sd)
	}

	// School level

	egen formalparti = rowmean(S_comm_members_z2 S_comm_z2 S_board_parent_rep_z2 P_assembly_z2)
	egen formalparti_rr = rowmean(S_comm_members_z2 S_comm_z2 S_board_parent_rep_z2)
	egen directoronT = rowmean(D_perf_teachers_z2 D_problem19_z2 D_problem21_z2 D_problem22_z2)
	egen publicinfo = rowmean(S_info1_z2 S_info2_z2 S_info3_z2 S_info4_z2 S_info5_z2 S_info6_z2 S_info7_z2 S_info8_z2)
	egen safety = rowmean(I_classrooms_improvement_z2 I_security_z2 I_fence_z2 I_fence_impr_const_z2)
	egen higiene = rowmean(I_cleaner_z2 bathroom_students_z2 A_water_z2)
	egen directoronF = rowmean(D_problem4_z2 D_problem6_z2 D_problem7_z2 D_problem8_z2 D_problem28_z2 D_problem29_z2 D_problem30_z2 D_problem31_z2 D_problem32_z2)
	egen material = rowmean(I_chair_desk_usable_sh_z2 S_new_exerbooks_z2 S_new_chalk_z2)

	foreach var in  A_repeaters formalparti formalparti_rr directoronT publicinfo safety higiene directoronF material {
	sum `var' if regschool==1 & t==1 & T0==1
	gen `var'_z = (`var'-r(mean))/r(sd)
	}

	// This index is made up out of different surveys.

	sort school_id t
	by school_id t: egen parentsonF_zX = mean(parentsonF_z) if regparent==1
	by school_id t: egen parentsonF_zXX = max(parentsonF_zX)

	by school_id t: egen teacheronF_zX = mean(teacheronF_z) if regteacher==1
	by school_id t: egen teacheronF_zXX = max(teacheronF_zX)

	egen onF = rowmean(directoronF_z parentsonF_zXX teacheronF_zXX)

	foreach var in onF{
	sum `var' if regschool==1 & t==1 & T0==1
	gen `var'_z = (`var'-r(mean))/r(sd)
	}

	// Scorecard

	sort school_id
	forval x=0/1{
		gen sc_full_new`x' = sc_full if regschool==1 & t==`x'
		sum sc_full_new`x'
		gen scorecard_z_`x' = (sc_full_new`x'-r(mean))/r(sd)
		by school_id: egen scorecard_z_`x's = max(scorecard_z_`x')
	}

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Baseline parent mobilization

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*


	egen basebal_parmeet = rowmean(basebal_TD3base_10_00 basebal_TD3base_10_01 basebal_TD3base_10_02 basebal_TD3base_10_03 basebal_TD3base_10_04 basebal_TD3base_10_05 basebal_TD3base_10_06)

	foreach var in basebal_PQ4_04 basebal_PQ4_05 basebal_PQ4_06 basebal_PQ4_25 basebal_TD7base_04 basebal_TD7base_05 basebal_TD7base_06 basebal_TD7base_25{
		sum `var'
		gen `var'_z = (`var'-r(mean))/r(sd)
	}


	egen basebal_PonF_X = rowmean(basebal_PQ4_04_z basebal_PQ4_05_z basebal_PQ4_06_z basebal_PQ4_25_z)
	egen basebal_TonF_X = rowmean(basebal_TD7base_04_z basebal_TD7base_05_z basebal_TD7base_06_z basebal_TD7base_25_z)

	sort school_id
	by school_id: egen basebal_PonF = mean(basebal_PonF_X)
	by school_id: egen basebal_TonF = mean(basebal_TonF_X)

	foreach var in S_info1 S_info2 S_info3 S_info4 S_info5 S_info6 S_info7 S_info8 basebal_TD7base_04_dir basebal_TD7base_05_dir basebal_TD7base_06_dir basebal_TD7base_25_dir{
		sum `var' if regschool==1 & t==0
		gen `var'_z0 = (`var'-r(mean))/r(sd)
	}

	egen basebal_publicinfo_X = rowmean(S_info1_z0 S_info2_z0 S_info3_z0 S_info4_z0 S_info5_z0 S_info6_z0 S_info7_z0 S_info8_z0)
	gen basebal_publicinfo = basebal_publicinfo_X if regschool==1 & t==0

	egen basebal_DonF = rowmean(basebal_TD7base_04_dir_z0 basebal_TD7base_05_dir_z0 basebal_TD7base_06_dir_z0 basebal_TD7base_25_dir_z0)
	egen basebal_onF_X = rowmean(basebal_PonF basebal_TonF basebal_DonF)
	gen basebal_onF = basebal_onF_X if regschool==1 & t==0

	egen basebal_boardparX = anymatch(basebal_SQ8_02_01 basebal_SQ8_02_02 basebal_SQ8_02_03 basebal_SQ8_02_04 basebal_SQ8_02_05 basebal_SQ8_02_06 basebal_SQ8_02_07 basebal_SQ8_02_08 basebal_SQ8_02_09 basebal_SQ8_02_10), v(1)
	gen basebal_boardpar = basebal_boardparX if regschool==1 & t==0

	egen basebal_bath_X = rowtotal(basebal_SQ2_06_a basebal_SQ2_07_a), missing
	gen basebal_bath = basebal_bath_X if regschool==1 & t==0
	replace basebal_bath = 1 if basebal_bath>0 & basebal_bath!=.

	gen basebal_con = basebal_SQ2_14 if regschool==1 & t==0

	gen basebal_water = basebal_SQ2_09_dummy if regschool==1 & t==0
	gen basebal_desk = basebal_SQ3_desks if regschool==1 & t==0
	gen basebal_chairs = basebal_SQ3_chairs if regschool==1 & t==0
	gen basebal_excbooks = basebal_SQ3_books_exc if regschool==1 & t==0
	replace basebal_excbooks = . if basebal_excbooks==999

	recode basebal_PQ6_12 (6=0)(5=1)(4=2)(2=4)(1=5)
	recode basebal_PQ6_13 (6=0)(5=1)(4=2)(2=4)(1=5)
	recode basebal_PQ3_12 (6=1)(5=2)(4=3)(3=4)(2=5)(1=6)
	recode basebal_PQ9_09 (5=1)(4=2)(3=3)(2=4)(1=5)
	recode basebal_PQ7_07 (4=1)(3=2)(2=3)(1=4)
	recode basebal_TD7base_14 (3=0)(2=1)(1=2)(0=3)
	recode basebal_PQ4_16 (3=0)(2=1)(1=2)(0=3)

	replace basebal_onF = basebal_onF*(-1)


	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Games

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	sort school_id
	by school_id: egen version = mean(Gt_treatment)

  // Create dictator game variables for each version

  forval i = 2/3{
    gen Gt_dg_r`i' = Gt_dg_r
    replace Gt_dg_r`i' = . if version!=`i'
    foreach var in Gp_dg_faces0 Gp_dg_faces500 Gp_dg_faces1000 Gp_dg_faces1500 Gp_dg_faces2000{
      gen `var'_`i' = `var'
      replace `var'_`i' = . if version!=`i'
    }
  }


	replace Gp_dg_faces0_2 = (Gp_dg_faces0_2/5)*100
	replace Gp_dg_faces0_3 = Gp_dg_faces0_3*100

	gen Gp_dgf0_2 = Gp_dg_faces0_2
	gen Gp_dgf0_3 = Gp_dg_faces0_3

	bysort school_id parent_id: gen n = _n
	replace Gp_dg_faces0_2=. if n!=1
	drop n




  bysort school_id teacher_id: gen n = _n
  replace Gt_dg=. if n!=1
  drop n

	replace Gp_tg = Gp_tg/800

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Heterogenous effects specification

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	sort school_id t

	by school_id t: egen educlevel1 = mean(P_primedu)
	gen educlevel0X = educlevel1 if t==0
	by school_id: egen educlevel0 = max(educlevel0X)

	forval t = 1/3{
		forval x = 0/1{
			gen T`t'xeduclevel`x' = T`t'*educlevel`x'
		}
	}

	sort school_id
	forval x=2/5{
		egen sc_cat`x' = cut(sc_full) if t==1 & regschool==1, group(`x')
		by school_id: egen sc_cat`x'x = mean(sc_cat`x')
		replace sc_cat`x' = sc_cat`x'x
		drop sc_cat`x'x
	}

	egen educ_cat3 = cut(educlevel0) if t==1 & regschool==1, group(3)
	by school_id: egen educ_cat3x = mean(educ_cat3)
	replace educ_cat3 = educ_cat3x
	drop educ_cat3x

  tab sc_cat3, gen(top_perf)
    rename top_perf3 top_perf
    drop top_perf1 top_perf2

  tab educ_cat3, gen(low_educ)
    rename low_educ1 low_educ
    drop low_educ2 low_educ3

  foreach inter in top_perf low_educ{
	  forval i=1/3{
	    gen T`i'x`inter' = T`i'*`inter'
	  }
  }

	foreach var in T1 T2 T3{
	  gen `var'xP_female = `var'*P_female
	  lab var `var'xP_female "`var' x Parent is female"
	}




	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*

	* Manage labels

	*																																						*
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	*																																						*


	foreach var of varlist _all {
		label var `var' ""
	}

	lab var T0 "T0: Control"
	lab var T1 "T1: Information"
	lab var T2 "T2: Collective action"
	lab var T3 "T3: Combined"

	lab var T_age "Teacher age"
	lab var T_female "Teacher is female"
	lab var T_highedu "Teacher with higher education"
	lab var T_partner "Teacher has partner"
	lab var T_children "Teacher number of children at home"
	lab var T_teacher_preschool "Teacher of preschool"
	lab var T_teacher_grade_1 "Teacher of grade 1"
	lab var T_teacher_grade_2 "Teacher of grade 2"
	lab var T_teacher_grade_3 "Teacher of grade 3"
	lab var T_teacher_grade_4 "Teacher of grade 4"
	lab var T_teacher_grade_5 "Teacher of grade 5"
	lab var T_teacher_grade_6 "Teacher of grade 6"
	lab var T_teacher_grade_7 "Teacher of grade 7"
	lab var T_teacher_grade_8 "Teacher of grade 8"
	lab var T_teacher_grade_9 "Teacher of grade 9"
	lab var T_teacher_module1 "Teacher of grade 10"
	lab var T_teacher_module2 "Teacher of grade 11"
	lab var T_teacher_module3 "Teacher of grade 12"
	lab var T_exp_teach "Teacher years experience"
	lab var P_age "Parent age"
	lab var P_female "Parent is female"
	lab var P_primedu "Parent primary education"
	lab var P_partner "Parent has partner"
	lab var P_children "Parent number of children at home"
	lab var P_student_age "Student age"
	lab var P_student_female "Student is female"
	lab var P_parent "Biological parent"
	lab var P_student_grade_3 "Student of grade 3"
	lab var P_student_grade_4 "Student of grade 4"
	lab var P_student_grade_5 "Student of grade 5"
	lab var teachers "Number of teachers"
	lab var classrooms "Number of classrooms"
	lab var students "Number of students"

	lab var teachersbaseline "Number of teachers at baseline"
	lab var classroomsbaseline "Number of classrooms at baseline"
	lab var studentsbaseline "Number of students at baseline"

	label define treatname 0 "C: Control" 1 "T1: Information" 2 "T2 Meetings" 3 "T3: Combined"
	label val treatment treatname

********************************************************************************


	save "$Endline_dtFin/data_angolacbm.dta", replace
